package com.yfqy.app.security;

import cn.hutool.json.JSONUtil;
import com.yfqy.app.constant.GeneralConstants;
import com.yfqy.app.domain.vo.ResultVO;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.NonNull;
import org.springframework.http.HttpStatus;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;
import java.io.PrintWriter;

public class InvalidRequestFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(@NonNull HttpServletRequest request,
                                    HttpServletResponse response,
                                    @NonNull FilterChain filterChain) throws ServletException, IOException {
        response.setContentType(GeneralConstants.CHARSET_UTF8);
        response.setStatus(HttpStatus.FORBIDDEN.value());
        PrintWriter writer = response.getWriter();
        writer.print(JSONUtil.toJsonStr(ResultVO.fail("invalid url")));
        writer.flush();
        writer.close();
    }
}
